/**
 * Created by 顾杰 on 2017/9/6.
 */
import React from 'react';
import {connect} from 'dva';
import {List} from 'antd-mobile';
import CardItem from './index';
import styles from './tree.less';

const Item = List.Item;
const Brief = Item.Brief;
const empty = require('is-empty');
class TreeItem extends React.Component {

  constructor(props) {
    super(props);
    this.state = {
    };
  }

  componentWillMount = () => {
    this.props.dispatch({
      type: 'witTree/fetch',
      payload: {
        code: this.props.code
      }
    })
  };

  go = (code) => {
    this.props.go(code);
  };

  render() {
    const {tree} = this.props.witTree;
    if(empty(tree)) {
      return false;
    }
    return (
      <div>
        <CardItem
          header={this.props.header}
          body={
            <div>
              {
                tree.map((item, key) => (
                  <Item key={key} arrow="horizontal" onClick={() => this.go(item.Code)}>
                    {item.Name}
                    <Brief>{item.TypeName}</Brief>
                  </Item>
                ))
              }
            </div>
          }
        />
      </div>
    )
  }
}

function mapStateToProps({witTree}){
  return {witTree};
}
export default connect(mapStateToProps)(TreeItem);
